Component({
  mixins: [],
  data: {
    imageUrl: [],
    maxCount: 1,
    action: '', //上传的url地址
  },
  props: {
    maxCount: 1,
    onUpload: (image, imageArr) => {},
    onDelete: (image, imageArr) => {},
    value: [],
    action: '',
  },
  didMount() {
    const maxCount = this.props?.maxCount || 1
    const value = this.props?.value || []
    const action =
      this.props?.action ||  'xxxxxxxx'
    this.setData({
      maxCount,
      imageUrl: value,
      action,
    })
  },
  didUpdate() {},
  didUnmount() {},
  methods: {
    handleSelectImage() {
      const { imageUrl, maxCount } = this.data
      if (imageUrl.length == maxCount) {
        my.showToast({
          content: `最多上传${maxCount}张图片`,
        })
        return
      }
      my.chooseImage({
        sourceType: ['camera', 'album'],
        count: 1,
        success: (res) => {
          const path = res.apFilePaths[0]
          const DeviceId = my.getStorageSync({ key: 'DeviceId' }).data
          const ACCESS_TOKEN = my.getStorageSync({ key: '__AT__' }).data
          const { account } = my.getStorageSync({ key: 'userinfo' }).data as any
          const { action } = this.data
          const SYSTEM_ID =
            (my.getStorageSync({ key: 'SyStemId' }).data as string)
          my.uploadFile({
            url: action,
            fileType: 'image',
            fileName: 'file',
            filePath: path,
            name: 'file',
            //请求头信息
            header: {
              DeviceId: DeviceId,
              uname: account,
              System: SYSTEM_ID,
            },
            success: (res) => {
              const value = JSON.parse(res.data)
              if (value?.code == 200) {
                const { imageUrl } = this.data
                const newImageUrl = imageUrl.concat(value?.data)
                this.setData({
                  imageUrl: newImageUrl,
                })
                this.props.onUpload(value?.data, newImageUrl)
              } else {
                my.showToast({
                  content: value?.msg || '上传失败',
                })
              }
            },
            fail: (err) => {
              my.showToast({
                content: '上传失败',
              })
              const { imageUrl } = this.data
              const newImageUrl = imageUrl.concat([])
              this.setData({
                imageUrl: newImageUrl,
                status: 'fail',
              })
            },
          })
        },
      })
    },
    handleDelete(e) {
      const { url } = e.currentTarget.dataset
      const { imageUrl } = this.data
      const newData = imageUrl.filter((i) => i !== url)
      this.setData({
        imageUrl: newData,
      })
      this.props.onDelete(index, newData)
    },
    hanldeImageLook(e) {
      const { imageUrl } = this.data
      const { index } = e.currentTarget.dataset
      my.previewImage({
        current: index, // 当前显示图片
        urls: imageUrl,
        success: (res) => {
          console.log('debug----res', res)
        },
        fail: (error) => {
          console.log('debug----error', error)
        },
      })
    },
  },
})
